<?php
/*
 * 会员类
 * @arrange Mark
 * @package ixdcw
 * @file    member.class.php
 * @time    12-12-19 上午11:48
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 会员操作类
 */
class member {

	/**
	 * @var 用户id
	 */
	public $userid;

	/** @var 模块id */
	private $_moduleid;

	/**
	 * @var 用户名
	 */
	public $username;

	/**
	 * @var 数据库连接类
	 */
	public $db;

	/**
	 * @var string member表
	 */
	public $table_member;

	/**
	 * @var string company 表
	 */
	public $table_company;

	/**
	 * @var string company_catid表
	 */
	public $table_company_catid;


	/**
	 * @var string  company_data表
	 */
	public $table_company_data;


	/**
	 * @var string  调用常量的显示, 错误的提示
	 */
	public $errmsg = errmsg;

	function __construct() {
		global $db, $moduleid;
		$this->_moduleid           = $moduleid;
		$this->table_member        = $db->getPre() . 'member';
		$this->table_company       = $db->getPre() . 'company';
		$this->table_company_catid = $db->getPre() . 'company_catid';
		$this->table_company_data  = $db->getPre() . 'company_data';
		$this->db                  = & $db;
	}

	/**
	 * @param $username
	 * @return bool 用户名是否可用, 或者符合规则
	 */
	function is_username($username) {
		global $MOD, $L;
		if (!User::checkname($username)) return $this->_($L['member_username_match']);
		$MOD['minusername'] or $MOD['minusername'] = 4;
		$MOD['maxusername'] or $MOD['maxusername'] = 20;
		if (strlen($username) < $MOD['minusername'] || strlen($username) > $MOD['maxusername']) return $this->_(lang($L['member_username_len'], array($MOD['minusername'], $MOD['maxusername'])));
		if ($MOD['banusername']) {
			$tmp = explode('|', $MOD['banusername']);
			foreach ($tmp as $v) {
				if ($MOD['banmodeu']) {
					if ($username == $v) return $this->_($L['member_username_ban']);
				} else {
					if (strpos($username, $v) !== false) return $this->_($L['member_username_ban']);
				}
			}
		}
		if ($this->username_exists($username)) return $this->_($L['member_username_reg']);
		return true;
	}

	function has_company($companyName) {
		global $db;
		$sql = "select * from " . $db->getPre() . "company where company='{$companyName}'";
		if ($db->getVar($sql)) {
			$this->_('已存在的公司名称');
			return true;
		}
		return false;
	}

	function is_company($company) {
		global $MOD, $L;
		$company = trim($company);
		if ($MOD['bancompany']) {
			$tmp = explode('|', $MOD['bancompany']);
			foreach ($tmp as $v) {
				if ($MOD['banmodec']) {
					if ($company == $v) return $this->_($L['member_company_ban']);
				} else {
					if (strpos($company, $v) !== false) return $this->_($L['member_company_ban']);
				}
			}
		}
		return true;
	}

	function is_email($email) {
		global $MOD, $L;
		$email = trim($email);
		//if(!is_email($email)) return $this->_($L['member_email_null']);
		if ($MOD['banemail']) {
			$domain = substr(strstr($email, '@'), 1);
			$tmp    = explode('|', $MOD['banemail']);
			foreach ($tmp as $v) {
				if ($domain == $v) return $this->_($L['member_email_ban']);
			}
		}
		return true;
	}

	function is_passport($passport) {
		global $MOD, $L;
		$MOD['minusername'] or $MOD['minusername'] = 4;
		$MOD['maxusername'] or $MOD['maxusername'] = 20;
		if (strlen($passport) < $MOD['minusername'] || strlen($passport) > $MOD['maxusername']) return $this->_(lang($L['member_passport_len'], array($MOD['minusername'], $MOD['maxusername'])));
		$badwords = array("$", "\\", '&', ' ', "'", '"', '/', '*', ',', '<', '>', "\r", "\t", "\n", "#");
		foreach ($badwords as $v) {
			if (strpos($passport, $v) !== false) return $this->_($L['member_passport_char']);
		}
		if ($MOD['banusername']) {
			$tmp = explode('|', $MOD['banusername']);
			foreach ($tmp as $v) {
				if ($MOD['banmodeu']) {
					if ($passport == $v) return $this->_($L['member_passport_ban']);
				} else {
					if (strpos($passport, $v) !== false) return $this->_($L['member_passport_ban']);
				}
			}
		}
		if ($this->passport_exists($passport)) return $this->_($L['member_passport_reg']);
		return true;
	}

	function is_password($password, $cpassword) {
		global $MOD, $L;
		if (!$password) return $this->_($L['member_password_null']);
		if ($password != $cpassword) return $this->_($L['member_password_match']);
		if (!$MOD['minpassword']) $MOD['minpassword'] = 6;
		if (!$MOD['maxpassword']) $MOD['maxpassword'] = 20;
		if (strlen($password) < $MOD['minpassword'] || strlen($password) > $MOD['maxpassword']) return $this->_(lang($L['member_password_len'], array($MOD['minpassword'], $MOD['maxpassword'])));
		return true;
	}

	function is_payword($password, $cpassword) {
		global $MOD, $L;
		if (!$password) return $this->_($L['member_payword_null']);
		if ($password != $cpassword) return $this->_($L['member_payword_match']);
		if (!$MOD['minpassword']) $MOD['minpassword'] = 6;
		if (!$MOD['maxpassword']) $MOD['maxpassword'] = 20;
		if (strlen($password) < $MOD['minpassword'] || strlen($password) > $MOD['maxpassword']) return $this->_(lang($L['member_payword_len'], array($MOD['minpassword'], $MOD['maxpassword'])));
		return true;
	}

	function is_member($member) {
		global $L, $AREA, $MG;
		if (!is_array($member)) return false;
		if (!$this->is_passport($member['passport'])) return false;
		if (!$member['groupid']) return $this->_($L['member_groupid_null']);
//		if(empty($member['truename'])) return $this->_($L['member_truename_null']);
		//if(!$this->is_email(trim($member['email']))) return false;
		//if($this->email_exists(trim($member['email']))) return $this->_($L['member_email_reg']);
		$areaid = intval($member['areaid']);
//		if(!$areaid || !$this->db->getOne("SELECT areaid FROM {$this->db->pre}area WHERE areaid=$areaid")) return $this->_($L['member_areaid_null']);
		$groupid = $this->userid ? $member['groupid'] : $member['regid'];
		if ($MG['memtype'] >= 5) {
			if (strlen($member['company']) < 2) return $this->_($L['member_company_null']);
			if (preg_match("/[0-9]{5,}/", $member['company'])) return $this->_($L['member_company_bad']);
//			if($this->company_exists($member['company'])) return $this->_($L['member_company_reg']);
			if (strlen($member['telephone']) < 6) return $this->_($L['member_telephone_null']);
		}
		if ($this->userid) {
			if ($member['password'] && !$this->is_password($member['password'], $member['cpassword'])) return false;
			if ($member['payword'] && !$this->is_payword($member['payword'], $member['cpayword'])) return false;
			if ($MG['memtype'] >= 5) { // company
				if (strlen($member['regyear']) != 4 || !is_numeric($member['regyear'])) return $this->_($L['member_regyear_null']);
				if (empty($member['address'])) return $this->_($L['member_address_null']);
//				if(Str::length($member['introduce']) < 5) return $this->_($L['member_introduce_null']);
				if (count($member['catid']) < 1) return $this->_($L['member_catid_null']);
			}
		} else {
			if (!$this->is_username($member['username'])) return false;
			if ($member['groupid'] > 5 && !$this->is_company($member['company'])) return false;
			if (!$this->is_password($member['password'], $member['cpassword'])) return false;
		}
		return true;
	}

	function set_member($member) {
		global $MOD, $CATEGORY;
		$thumbSeName = $this->_moduleid . '_thumb';
		$CATEGORY or $CATEGORY = PCache::read('category-4.php');
		$member['email'] = trim($member['email']);
		$member['mail']  = isset($member['mail']) ? trim($member['mail']) : '';
		is_email($member['mail']) or $member['mail'] = '';
		$member['msn'] = isset($member['msn']) ? trim($member['msn']) : '';
		is_email($member['msn']) or $member['msn'] = '';
		$member['qq'] = isset($member['qq']) ? trim($member['qq']) : '';
		is_numeric($member['qq']) or $member['qq'] = '';
		$member['postcode'] = isset($member['postcode']) ? trim($member['postcode']) : '';
		is_numeric($member['postcode']) or $member['postcode'] = '';
		$member['keyword']  = $member['company'];
		$member['homepage'] = isset($member['homepage']) ? fix_link($member['homepage']) : '';
		$member['capital']  = isset($member['capital']) ? dround($member['capital']) : '';
		$member['sound']    = intval($member['sound']);
		if ($member['arbid']) {
			foreach ($member['arbid'] as $v) {
				if (!preg_match("/^\d*$/", $v)) {
					$member['arbid'] = '';
				}
			}
			sort($member['arbid']);
			$member['arbid'] = implode(',', $member['arbid']);
			$sql             = 'select distinct(acid) from xd_auto_model where arbid in (' . $member['arbid'] . ')';
			$acid            = $this->db->getAll($sql);
			$level           = PCache::read('autoCategory-level.php');
			foreach ($acid as $v) {
				if ($v['acid'] != 0) {
					$member['acid'][] = $v['acid'];
					$member['acid'][] = $level[$v['acid']]['parentid'];
				}
			}
			$member['acid'] = array_flip(array_flip($member['acid']));
			sort($member['acid']);
			$member['acid'] = implode(',', $member['acid']);
		} else {
			$member['arbid'] = '';
			$member['arcid'] = '';
			$member['amid']  = '';
		}
		if ($member['arcid'] && $member['arbid']) {
			foreach ($member['arcid'] as $v) {
				if (!preg_match("/^\d*$/", $v)) {
					$member['arcid'] = '';
				}
			}
			sort($member['arcid']);
			$member['arcid'] = implode(',', $member['arcid']);
		} else {
			$member['arcid'] = '';
			$member['amid']  = '';
		}
		if ($member['arcid'] && $member['arbid'] && $member['amid']) {
			foreach ($member['amid'] as $v) {
				if (!preg_match("/^\d*$/", $v)) {
					$member['amid'] = '';
				}
			}
			sort($member['amid']);
			$member['amid'] = implode(',', $member['amid']);
		} else {
			$member['amid'] = '';
		}

		if ($this->userid) {
			$member['keyword'] = $member['company'] . strip_tags(area_pos($member['areaid'], ',')) . ',' . $member['business'] . ',' . $member['sell'] . ',' . $member['buy'];
			$new               = $member['introduce'];
			$content_table     = content_table(4, $this->userid, is_file(DT_CACHE . '/4.part'), $this->table_company_data);
			$r                 = $this->db->getOne("SELECT content FROM {$content_table} WHERE userid=$this->userid");
			$old               = $r['content'];
			$r                 = $this->get_one();
			ThumbUpload::clearPicture($this->_moduleid . '_thumb', $member['thumb'], $r['thumb'], $this->userid);
			EditorUpload::clearDiff($new, $old, $this->userid);
		} else {
			ThumbUpload::clearPicture($this->_moduleid . '_thumb', $member['thumb'], '', $this->userid);
		}
		$member['content']   = $member['introduce'];
		$member['introduce'] = addslashes(get_intro($member['content'], $MOD['introduce_length']));
		if (!defined('DT_ADMIN')) {
			$content = $member['content'];
			unset($member['content']);
			$member            = Str::htmlSpecialChars($member);
			$member['content'] = Str::safe($content);
		}
		if ($MOD['introduce_clear'] || $MOD['introduce_save']) {
			$member['content'] = stripslashes($member['content']);
			if ($MOD['introduce_clear']) $member['content'] = Post::clearLink($member['content']);
			if ($MOD['introduce_save']) $member['content'] = Post::saveRemote($member['content']);
			$member['content'] = addslashes($member['content']);
		}
		if ($member['catid']) {
			$catids = $member['catid'];
			$cids   = '';
			foreach ($catids as $catid) {
				if (isset($CATEGORY[$catid])) {
					$catid = $CATEGORY[$catid]['parentid'] ? $CATEGORY[$catid]['arrparentid'] . ',' . $catid : $catid;
					$cids .= $catid . ',';
				}
			}
			$cids             = array_unique(explode(',', substr(str_replace(',0,', ',', ',' . $cids), 1, -1)));
			$member['catids'] = ',' . implode(',', $cids) . ',';
			$member['catid']  = implode(',', $member['catid']);
		}
		return $member;
	}

	function email_exists($email) {
		$condition = "email='$email'";
		if ($this->userid) $condition .= " AND userid!=$this->userid";
		return $this->db->getOne("SELECT userid FROM {$this->table_member} WHERE $condition");
	}

	/**
	 * @param $username
	 * @return mixed 是否存在这个用户名
	 */
	function username_exists($username) {
		return $this->db->getOne("SELECT userid FROM {$this->table_member} WHERE username='$username'");
	}

	function company_exists($company) {
		$condition = "company='$company'";
		if ($this->userid) $condition .= " AND userid!=$this->userid";
		return $this->db->getOne("SELECT userid FROM {$this->table_company} WHERE $condition");
	}

	function passport_exists($passport) {
		$condition = "passport='$passport'";
		if ($this->userid) $condition .= " AND userid!=$this->userid";
		return $this->db->getOne("SELECT userid FROM {$this->table_member} WHERE $condition");
	}

	function add($member) {

		global $DT, $DT_TIME, $DT_IP, $MOD, $L;
		if (!$this->is_member($member)) return false;


		$member             = $this->set_member($member);
		$member['linkurl']  = userurl($member['username']);
		$member['password'] = $member['payword'] = md5(md5($member['password']));
		$member['sound']    = 1;

		$member_fields  = array('username', 'company', 'passport', 'password', 'payword', 'email', 'sound', 'gender', 'truename', 'mobile', 'msn', 'qq', 'ali', 'skype', 'department', 'career', 'groupid', 'regid', 'areaid', 'edittime', 'inviter');
		$company_fields = array('username', 'acid', 'arbid', 'arcid', 'amid', 'groupid', 'company', 'type', 'catid', 'catids', 'areaid', 'capital', 'regunit', 'size', 'regyear', 'sell', 'buy', 'business', 'telephone', 'fax', 'mail', 'address', 'postcode', 'homepage', 'introduce', 'thumb', 'keyword', 'linkurl');
		$member_sqlk    = $member_sqlv = $company_sqlk = $company_sqlv = '';
		foreach ($member as $k => $v) {
			if (in_array($k, $member_fields)) {
				$member_sqlk .= ',' . $k;
				$member_sqlv .= ",'$v'";
			}
			if (in_array($k, $company_fields)) {
				$company_sqlk .= ',' . $k;
				$company_sqlv .= ",'$v'";
			}
		}
		$member_sqlk  = substr($member_sqlk, 1);
		$member_sqlv  = substr($member_sqlv, 1);
		$company_sqlk = substr($company_sqlk, 1);
		$company_sqlv = substr($company_sqlv, 1);
		$this->db->query("INSERT INTO {$this->table_member} ($member_sqlk,regip,regtime,loginip,logintime)  VALUES ($member_sqlv,'$DT_IP','$DT_TIME','$DT_IP','$DT_TIME')");
		$this->userid     = $this->db->insertId();
		$member['userid'] = $this->userid;
		$this->update_catid($member);
		$this->username = $member['username'];

//		if($member['regid']>5) {
		$this->db->query("INSERT INTO {$this->table_company} (userid, $company_sqlk) VALUES ('$this->userid', $company_sqlv)");
		$content_table = content_table(4, $this->userid, is_file(DT_CACHE . '/4.part'), $this->table_company_data);
		$this->db->query("INSERT INTO {$content_table} (userid, content) VALUES ('$this->userid', '$member[content]')");
//		}
		if ($MOD['credit_register'] > 0) {
			credit_add($this->username, $MOD['credit_register']);
			credit_record($this->username, $MOD['credit_register'], 'system', $L['member_record_reg'], $DT_IP);
		}
		if ($MOD['money_register'] > 0) {
			money_add($this->username, $MOD['money_register']);
			money_record($this->username, $MOD['money_register'], $L['in_site'], 'system', $L['member_record_reg'], $DT_IP);
		}
		if ($MOD['sms_register'] > 0) {
			sms_add($this->username, $MOD['sms_register']);
			sms_record($this->username, $MOD['sms_register'], 'system', $L['member_record_reg'], $DT_IP);
		}
		return $this->userid;
	}

	function edit($member) {
		if (!$this->is_member($member)) return false;
		$member            = $this->set_member($member);
		$r                 = $this->get_one();
		$member['linkurl'] = userurl($r['username'], '', $member['domain']);
		$member_fields     = array('company', 'passport', 'sound', 'email', 'msn', 'qq', 'ali', 'skype', 'gender', 'truename', 'mobile', 'department', 'career', 'groupid', 'areaid', 'edittime', 'black', 'bank', 'account', 'vemail', 'vmobile', 'vbank', 'vtruename', 'vcompany', 'vtrade', 'trade', 'support', 'inviter', 'attention','cityid');
		$company_fields    = array('company', 'acid', 'arbid', 'arcid', 'amid', 'type', 'areaid', 'catid', 'catids', 'business', 'regyear', 'regunit', 'capital', 'size', 'address', 'postcode', 'telephone', 'fax', 'mail', 'homepage', 'sell', 'buy', 'introduce', 'thumb', 'keyword', 'linkurl', 'groupid', 'domain', 'icp',
			'validated',
			'validator',
			'validtime',
			'skin', 'template');
		$member_sql        = $company_sql = '';
		foreach ($member as $k => $v) {
			if (in_array($k, $member_fields)) $member_sql .= ",$k='$v'";
			if (in_array($k, $company_fields)) $company_sql .= ",$k='$v'";
		}
		if ($member['password']) {
			$password = md5(md5($member['password']));
			$member_sql .= ",password='$password'";
		}
		if ($member['payword']) {
			$payword = md5(md5($member['payword']));
			$member_sql .= ",payword='$payword'";
		}
		$member_sql  = substr($member_sql, 1);
		$company_sql = substr($company_sql, 1);
		$this->db->query("UPDATE {$this->table_member} SET $member_sql WHERE userid=$this->userid");
		$this->db->query("UPDATE {$this->table_company} SET $company_sql WHERE userid=$this->userid");
		$content_table = content_table(4, $this->userid, is_file(DT_CACHE . '/4.part'), $this->table_company_data);
		// fix un fulled content  2013-6-5 [mark]
		$contentIsExists = $this->db->getOne("select * from {$content_table} where userid={$this->userid}");
		if (!$contentIsExists) {
			$this->db->query("insert into {$content_table}(userid, content) values ('{$this->userid}', '{$member['content']}')");
		} else {
			$this->db->query("UPDATE {$content_table} SET content='$member[content]' WHERE userid=$this->userid");
		}
		$member['userid'] = $this->userid;
		$member['vip']    = $r['vip'];
		$this->update_catid($member);
		return true;
	}


	function get_one($username = '') {
		$condition = $username ? "m.username='$username'" : "m.userid='$this->userid'";
		$user      = $this->db->getOne("SELECT * FROM {$this->table_member} m,{$this->table_company} c WHERE m.userid=c.userid AND $condition");
		if ($user['attention']) {
			$user['attention'] = explode(',', $user['attention']);
		} else {
			$user['attention'] = array();
		}

		return $user;
	}

	function get_list($condition, $order = 'userid DESC') {
		global $pages, $page, $pagesize, $offset;
		$sql     = "SELECT COUNT(*) AS num FROM {$this->table_member} WHERE $condition";
		$r       = $this->db->getOne($sql);
		$pages   = pages($r['num'], $page, $pagesize);
		$members = array();
		$result  = $this->db->query("SELECT * FROM {$this->table_member} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while ($r = $this->db->fetchArray($result)) {
			$r['logindate'] = timetodate($r['logintime'], 5);
			$r['regdate']   = timetodate($r['regtime'], 5);
			$members[]      = $r;
		}
		return $members;
	}

	function getStoreList($condition = ' is_store = 1 ', $order = 'userid DESC') {
		global $pages, $page, $pagesize, $offset;
		$sql     = "SELECT COUNT(*) AS num FROM {$this->table_company} where $condition";
		$r       = $this->db->getOne($sql);
		$pages   = pages($r['num'], $page, $pagesize);
		$result  = $this->db->getAll("SELECT * FROM {$this->table_company} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		return $result;
	}

	/**
	 * 用户登陆
	 * @param      $login_username
	 * @param      $login_password
	 * @param int  $login_cookietime
	 * @param bool $admin
	 * @return bool|mixed
	 */
	function login($login_username, $login_password, $login_cookietime = 0, $admin = false) {
		global $DT_TIME, $DT_IP, $MOD, $L;
		if (!User::checkname($login_username)) return $this->_($L['member_login_username_bad']);
		if (!$MOD || !isset($MOD['login_times'])) $MOD = PCache::read('module-2.php');
		// login_times 登录失败次数限制
		// lock_hour 锁定登录
		$login_lock = ($MOD['login_times'] && $MOD['lock_hour']) ? true : false;
		$LOCK       = array();
		if ($login_lock) {
			$LOCK = PCache::read($DT_IP . '.php', 'ban');
			if ($LOCK) {
				if ($DT_TIME - $LOCK['time'] < $MOD['lock_hour'] * 3600) {
					if ($LOCK['times'] >= $MOD['login_times']) return $this->_(lang($L['member_login_ban'], array($MOD['login_times'], $MOD['login_times'])));
				} else {
					$LOCK = array();
					PCache::delete($DT_IP . '.php', 'ban');
				}
			}
		}
		// 用户信息
		$user = userinfo($login_username, '');
		if (!$user) {
			$this->lock($login_lock, $LOCK, $DT_IP, $DT_TIME);
			return $this->_($L['member_login_not_member']);
		}
		if (!$admin) {
			if ($user['password'] != (Utils::isMd5($login_password) ? md5($login_password) : md5(md5($login_password)))) {
				$this->lock($login_lock, $LOCK, $DT_IP, $DT_TIME);
				return $this->_($L['member_login_password_bad']);
			}
		}
		if ($user['groupid'] == 2) return $this->_($L['member_login_member_ban']);
		$userid = $user['userid'];

		if ($MOD['credit_login'] > 0 && timetodate($DT_TIME, 3) != timetodate($user['logintime'], 3)) {
			credit_add($login_username, $MOD['credit_login']);
			credit_record($login_username, $MOD['credit_login'], 'system', $L['member_record_login'], $DT_IP);
		}
		$cookietime = $DT_TIME + ($login_cookietime ? intval($login_cookietime) : 86400 * 7);
		$auth       = Crypt::encode($user['userid'] . "\t" . $user['username'] . "\t" . $user['groupid'] . "\t" . $user['password'] . "\t" . $user['admin'], md5(DT_KEY . $_SERVER['HTTP_USER_AGENT']));
		Cookie::set('auth', $auth, $cookietime);
		Cookie::set('username', $user['username'], $DT_TIME + 86400 * 365);
		$this->db->query("UPDATE {$this->table_member} SET loginip='$DT_IP',logintime=$DT_TIME,logintimes=logintimes+1 WHERE userid=$userid");
		return $user;
	}

	/**
	 * Lock锁定, 用户的时间和登陆的次数
	 * @param $login_lock
	 * @param $LOCK
	 * @param $DT_IP
	 * @param $DT_TIME
	 */
	function lock($login_lock, $LOCK, $DT_IP, $DT_TIME) {
		if ($login_lock && $DT_IP) {
			$LOCK['time']  = $DT_TIME;
			$LOCK['times'] = isset($LOCK['times']) ? $LOCK['times'] + 1 : 1;
			PCache::write($DT_IP . '.php', $LOCK, 'ban');
		}
	}

	function logout() {
		Cookie::set('auth', '');
		return true;
	}

	/**
	 * 编辑讯都推荐
	 * @param $attention
	 * @return bool
	 */
	function editRecommend($attention) {
		return $this->db->query("UPDATE {$this->table_member} SET attention= '" . $attention . "' WHERE userid=$this->userid");
	}

	function delete($userid) {
		global $dc, $CFG, $MODULE, $L, $db;
		if (!$userid) return false;
		if (is_array($userid)) {
			if (in_array(1, $userid) || in_array($CFG['founderid'], $userid)) return $this->_($L['member_founder_del']);
			$userids = implode(',', $userid);
		} else {


			if ($userid == 1 || $userid == $CFG['founderid']) return $this->_($L['member_founder_del']);
			$userids = intval($userid);
		}
		$result = $this->db->query("SELECT username,userid FROM {$this->table_member} WHERE userid IN ($userids)");
		while ($r = $this->db->fetchArray($result)) {
			$userid   = $r['userid'];
			$username = $r['username'];
			$rl       = $this->get_one($username);
			if ($r['thumb']) PictureUpload::deletePicture($r['thumb'], $userid);
			if ($rl['introduce']) EditorUpload::clearDiff('', $r['content'], $userid);
			$content_table = content_table(4, $userid, is_file(DT_CACHE . '/4.part'), $this->table_company_data);
			$content_table = str_replace($db->getPre(), '', $content_table);
			foreach (array('alert', 'comment', 'finance_card', 'finance_cash', 'finance_charge', 'finance_credit', 'finance_pay', 'finance_record', 'finance_sms', 'guestbook', 'job_talent', 'link', 'admin_log', 'login', 'mail_list', 'upgrade', 'validate', 'address', 'oauth') as $v) {
				$this->deluser($v, $username, true);
			}
			foreach (array('news', 'resume') as $v) {
				$this->deluser($v, $username, true, true);
			}
			foreach ($MODULE as $m) {
				if ($m['islink'] || $m['moduleid'] < 5) continue;
				if (in_array($m['module'], array('article', 'info'))) {
					$this->deluser($m['module'] . '_' . $m['moduleid'], $username, true, true);
				} else {
					$this->deluser($m['module'], $username, true, true);
				}
			}
			$this->db->query("DELETE FROM {$db->getPre()}job_apply WHERE apply_username='$username'");
			$this->db->query("DELETE FROM {$db->getPre()}message WHERE fromuser='$username'");
			$this->db->query("DELETE FROM {$db->getPre()}message WHERE touser='$username'");
			$this->db->query("DELETE FROM {$db->getPre()}type WHERE item='friend-" . $userid . "'");
			$this->db->query("DELETE FROM {$db->getPre()}type WHERE item='favorite-" . $userid . "'");
			$this->db->query("DELETE FROM {$db->getPre()}type WHERE item='product-" . $userid . "'");
			$this->db->query("DELETE FROM {$db->getPre()}type WHERE item='news-" . $userid . "'");
			$this->db->query("DELETE FROM {$db->getPre()}type WHERE item='mall-" . $userid . "'");
			foreach (array('member', 'company', $content_table, 'company_setting', 'company_catid', 'admin', 'favorite', 'friend') as $v) {
				$this->deluser($v, $userid, false);
			}
			File::del(DT_CACHE . '/php/' . substr($username, 0, 2) . '/' . $username . '.php');
			$dc->rm(md5("SELECT * FROM {$db->getPre()}member m,{$db->getPre()}company c WHERE m.userid=c.userid AND m.username='$username' LIMIT 0,1"));
		}
		return true;
	}

	function deluser($table, $user, $name = true, $data = false) {
		global $db;
		$fields = $name ? 'username' : 'userid';
		if ($data) {
			$result = $this->db->query("SELECT itemid FROM {$db->getPre()}{$table} WHERE `$fields`='$user'");
			while ($r = $this->db->fetchArray($result)) {
				$itemid = $r['itemid'];
				$this->db->query("DELETE FROM {$db->getPre()}{$table} WHERE itemid='$itemid'");
				$table_data = strpos($table, '_') === false ? $table . '_data' : str_replace('_', '_data_', $table);
				$this->db->query("DELETE FROM {$db->getPre()}{$table_data} WHERE itemid='$itemid'");
				if ($table == 'sell') {
					$this->db->query("DELETE FROM {$db->getPre()}sell_search WHERE itemid=$itemid");
				}
			}
		} else {
			$this->db->query("DELETE FROM {$db->getPre()}{$table} WHERE `$fields`='$user'");
		}
	}

	function rename($cusername, $nusername) {
		global $db, $MODULE, $L;
		$cusername = trim($cusername);
		$nusername = trim($nusername);
		if (!$this->username_exists($cusername)) return $this->_($L['member_rename_not_member']);
		if (!$this->is_username($nusername)) return false;
		$tables = array('alert', 'ask', 'comment', 'honor', 'finance_card', 'finance_cash', 'finance_charge', 'finance_pay', 'finance_record', 'finance_sms', 'guestbook', 'job_talent', 'link', 'admin_log', 'login', 'mail_list', 'spread', 'news', 'resume', 'upgrade', 'know_answer', 'know_vote', 'news', 'page', 'address', 'oauth', 'vote_record', 'gift_order', 'poll_record', 'member', 'company');
		foreach ($MODULE as $m) {
			if ($m['islink'] || $m['moduleid'] < 5) continue;
			$tables[] = in_array($m['module'], array('article', 'info')) ? $m['module'] . '_' . $m['moduleid'] : $m['module'];
		}
		foreach ($tables as $table) {
			$this->db->query("UPDATE {$db->getPre()}{$table} SET username='$nusername' WHERE username='$cusername'");
		}
		$this->db->query("UPDATE {$db->getPre()}job_apply SET apply_username='$nusername' WHERE apply_username='$cusername'");
		$this->db->query("UPDATE {$db->getPre()}message SET fromuser='$nusername' WHERE fromuser='$cusername'");
		$this->db->query("UPDATE {$db->getPre()}message SET touser='$nusername' WHERE touser='$cusername'");
		return true;
	}

	function move($userid, $groupid) {
		global $CFG, $L;
		if (!isset($userid) || !$userid || !$groupid) return false;
		$userids = is_array($userid) ? implode(',', $userid) : intval($userid);
		if (is_array($userid)) {
			if (in_array(1, $userid) || in_array($CFG['founderid'], $userid)) return $this->_($L['member_founder_move']);
			$userids = implode(',', $userid);
		} else {
			if ($userid == 1 || $userid == $CFG['founderid']) return $this->_($L['member_founder_move']);
			$userids = intval($userid);
		}
		$this->db->query("UPDATE {$this->table_member} SET groupid='$groupid' WHERE userid IN ($userids)");
		$this->db->query("UPDATE {$this->table_company} SET groupid='$groupid' WHERE userid IN ($userids)");
		$this->db->query("UPDATE {$this->table_company_catid} SET groupid='$groupid' WHERE userid IN ($userids)");
		return true;
	}

	function check($userid) {
		if (is_array($userid)) {
			foreach ($userid as $v) {
				$this->check($v);
			}
		} else {
			$this->userid = $userid;
			$user         = $this->get_one();
			$groupid      = $user['regid'] ? $user['regid'] : 6;
			$this->db->query("UPDATE {$this->table_member} SET groupid=$groupid WHERE userid=$userid");
			$this->db->query("UPDATE {$this->table_company} SET groupid=$groupid WHERE userid=$userid");
			$this->db->query("UPDATE {$this->table_company_catid} SET groupid=$groupid WHERE userid=$userid");
			return true;
		}
	}

	function login_log($username, $password, $admin = 0, $message = '') {
		global $db, $DT_TIME, $DT_IP, $L;
		$password = Utils::isMd5($password) ? md5($password) : md5(md5($password));
		$agent    = $_SERVER['HTTP_USER_AGENT'];
		$message or $message = $L['member_login_ok'];
		if ($message == $L['member_login_ok']) PCache::delete($DT_IP . '.php', 'ban');
		$this->db->query("INSERT INTO {$db->getPre()}login (username,password,admin,loginip,logintime,message,agent) VALUES ('$username','$password','$admin','$DT_IP','$DT_TIME','$message','$agent')");
	}

	function update_catid($member = array()) {
		if ($member) {
			$userid  = $member['userid'];
			$groupid = intval($member['groupid']);
			$vip     = intval($member['vip']);
			$areaid  = intval($member['areaid']);
			$catids  = $member['catid'];
			$this->db->query("DELETE FROM {$this->table_company_catid} WHERE userid=$userid");
			if ($catids) {
				foreach (explode(',', $catids) as $catid) {
					$catid = intval($catid);
					if (!$catid) continue;
					$this->db->query("INSERT INTO {$this->table_company_catid} (userid,catid,areaid,groupid,vip) VALUES ($userid,$catid,$areaid,$groupid,$vip)");
				}
			}
		}
	}

	/**
	 * @param $e
	 * @return bool 错误赋值
	 */
	function _($e) {
		$this->errmsg = $e;
		return false;
	}
}

?>